package com.ruoyi.project.module.whd;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.library.api.ISysDictDataService;
import com.ruoyi.library.api.domain.SysDictData;
import com.ruoyi.project.domain.*;
import com.ruoyi.project.service.IHeEventBasicService;
import com.ruoyi.project.service.IHeEventFlowService;
import com.ruoyi.project.service.IHeEventReportService;

import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;

/**
 * 事件上报信息Controller
 * 
 * @author ruoyi
 * @date 2023-09-02
 */
@RestController
@RequestMapping("/whd/report")
@Api(value = "哈哈哈",tags = "啦啦啦",description = "哒哒哒")
public class WhdEventReportController extends BaseController
{
    @Autowired
    private IHeEventReportService heEventReportService;
    @Autowired
    private IHeEventBasicService heEventBasicService;
    @Autowired
    private IHeEventFlowService heEventFlowService;



    /**
     * 查询事件上报信息列表
     */
   /* @RequiresPermissions("project:report:list")*/
    @ApiOperation(value = "查询事件上报信息列表")
    @GetMapping("/list")
    public TableDataInfo list(HeEventReport heEventReport)
    {
        startPage();
        List<HeEventReport> list = heEventReportService.selectHeEventReportListByMonth(heEventReport);
        return getDataTable(list);
    }
    /*
    * 导出方法 plan 博
    *
    * */
    @PostMapping("/export/{list}")
    public void export(HttpServletResponse response, HeEventDto heEventDto, @PathVariable List<String> list) {
        List<HeEventDto> recordsList =  heEventReportService.selectHeEventDtoList(heEventDto);
        ExcelUtil<HeEventDto> util = new ExcelUtil<>(HeEventDto.class);
        Class<?> itemClass = HeEventDto.class;
        List<String> fieldNames = new ArrayList<>();
        Field[] fields = itemClass.getDeclaredFields();

        for (Field field : fields) {
            fieldNames.add(field.getName());
        }

        fieldNames.removeAll(list);
        util.hideColumn(fieldNames.toArray(new String[fieldNames.size()]));
        util.exportExcel(response, recordsList, "事件导出列表");
    }
    /*
     * 导出方法 plan 博
     *
     * */
    @PostMapping("/exportaudit/{list}")
    public void exportAudit(HttpServletResponse response, HeEventDto heEventDto, @PathVariable List<String> list) {
        List<HeEventDto> recordsList =  heEventReportService.selectAuditHeEventDtoList(heEventDto);
        ExcelUtil<HeEventDto> util = new ExcelUtil<>(HeEventDto.class);
        Class<?> itemClass = HeEventDto.class;
        List<String> fieldNames = new ArrayList<>();
        Field[] fields = itemClass.getDeclaredFields();

        for (Field field : fields) {
            fieldNames.add(field.getName());
        }

        fieldNames.removeAll(list);
        util.hideColumn(fieldNames.toArray(new String[fieldNames.size()]));
        util.exportExcel(response, recordsList, "事件导出列表");
    }
    /*
     * 导出方法 plan 博
     *
     * */
    @PostMapping("/exportdispose/{list}")
    public void exportDispose(HttpServletResponse response, HeEventDto heEventDto, @PathVariable List<String> list) {
        List<HeEventDto> recordsList =  heEventReportService.selectDisposeHeEventDtoList(heEventDto);
        ExcelUtil<HeEventDto> util = new ExcelUtil<>(HeEventDto.class);
        Class<?> itemClass = HeEventDto.class;
        List<String> fieldNames = new ArrayList<>();
        Field[] fields = itemClass.getDeclaredFields();

        for (Field field : fields) {
            fieldNames.add(field.getName());
        }

        fieldNames.removeAll(list);
        util.hideColumn(fieldNames.toArray(new String[fieldNames.size()]));
        util.exportExcel(response, recordsList, "事件导出列表");
    }
    /*
     * 导出方法 plan 博
     *
     * */
    @PostMapping("/exportstatistics/{list}")
    public void exportStatistics(HttpServletResponse response, HeEventDto heEventDto, @PathVariable List<String> list) {
        List<HeEventDto> recordsList =  heEventReportService.selectStatisticsHeEventDtoList(heEventDto);
        ExcelUtil<HeEventDto> util = new ExcelUtil<>(HeEventDto.class);
        Class<?> itemClass = HeEventDto.class;
        List<String> fieldNames = new ArrayList<>();
        Field[] fields = itemClass.getDeclaredFields();

        for (Field field : fields) {
            fieldNames.add(field.getName());
        }

        fieldNames.removeAll(list);
        util.hideColumn(fieldNames.toArray(new String[fieldNames.size()]));
        util.exportExcel(response, recordsList, "事件导出列表");
    }

    @GetMapping("/deptlist")
    public TableDataInfo deptList(HeEventReport heEventReport)
    {
        startPage();
        List<HeEventReport> list = heEventReportService.selectHeEventReportListBySupportDepartment(heEventReport);
        return getDataTable(list);
    }
    /**
     * 导出事件上报信息列表
     */
   /* @RequiresPermissions("project:report:export")*/
    @Log(title = "事件上报信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HeEventReport heEventReport)
    {
        List<HeEventReport> list = heEventReportService.selectHeEventReportList(heEventReport);
        ExcelUtil<HeEventReport> util = new ExcelUtil<HeEventReport>(HeEventReport.class);
        util.exportExcel(response, list, "事件上报信息数据");
    }

    /**
     * 获取事件上报信息详细信息
     */
    /*@RequiresPermissions("project:report:query")*/
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(heEventReportService.selectHeEventReportById(id));
    }

    /**
     * 新增事件上报信息
     */
    /*@RequiresPermissions("project:report:add")*/
    @Log(title = "事件上报信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HeReportEvent heReportEvent)
    {
        heReportEvent.getHeEventReport().setOccurrenceTime(heReportEvent.getHeEventBasic().getUndesirableTimeOccurrence());
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();

        String context=heReportEvent.getHeEventReport().getNote1();
        context=sysUser.getNickName()+"创建了"+context;
        heReportEvent.getHeEventReport().setNote1(null);
        HeEventReport heEventReport;
        heReportEvent.getHeEventReport().setReportEventState("01");
        heReportEvent.getHeEventReport().setReportEventNumber(DateUtils.getNowDate().getTime());
        heReportEvent.getHeEventBasic().setReportEventNumber(heReportEvent.getHeEventReport().getReportEventNumber());
        heReportEvent.getHeEventReport().setReportFiller(sysUser.getNickName());
        heReportEvent.getHeEventReport().setReportFillerId(sysUser.getUserId());
        heReportEvent.getHeEventReport().setReportDepartment(String.valueOf(sysUser.getDeptId()));
        //先新增report表
        //将创建时间注入到report表的creation_time字段中
        heEventReport=heReportEvent.getHeEventReport();
        heReportEvent.getHeEventReport().setCreationTime(DateUtils.getNowDate());
        heEventReportService.insertHeEventReport(heReportEvent.getHeEventReport());
        //查询已新增到report表的那条数据
        heEventReport=heReportEvent.getHeEventReport();
        //将新增的report表的id注入到basic表的report_event_id字段
        heReportEvent.getHeEventBasic().setReportEventId(heReportEvent.getHeEventReport().getId());
        //将创建时间注入到basic表的creation_time字段中
        heReportEvent.getHeEventBasic().setCreationTime(DateUtils.getNowDate());
        //同时注入到flow表
        //这个是将report表的id注入到flow的report_event_id字段
        heReportEvent.getHeEventFlow().setReportEventId(heEventReport.getId());
        heReportEvent.getHeEventFlow().setProcessOperatorId(sysUser.getUserId());
        heReportEvent.getHeEventFlow().setProcessOperatorName(sysUser.getNickName());
        heReportEvent.getHeEventFlow().setProcessOperateTime(DateUtils.dateTime());
        //这个是将report表的note1也就是事件类型注入到flow的process_stream_content字段
        heReportEvent.getHeEventFlow().setProcessStreamContent(context);
        //将创建时间注入到flow表的creation_time字段中
        heReportEvent.getHeEventFlow().setCreationTime(DateUtils.getNowDate());
        //新增flow表
        heEventFlowService.insertHeEventFlow(heReportEvent.getHeEventFlow());
        //新增basic表
        return toAjax(heEventBasicService.insertHeEventBasic(heReportEvent.getHeEventBasic()));
    }

    /**
     * 修改事件上报信息
     */
    /*@RequiresPermissions("project:report:edit")*/
    @Log(title = "事件上报信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HeReportEvent heReportEvent)
    {
        heReportEvent.getHeEventReport().setNote1(null);
        HeEventBasic heEventBasic=heReportEvent.getHeEventBasic();
        heEventBasicService.updateHeEventBasic(heEventBasic);
        HeEventReport heEventReport=heReportEvent.getHeEventReport();
        return toAjax(heEventReportService.updateHeEventReport(heEventReport));
    }

    /**
     * 删除事件上报信息
     */
  /*  @RequiresPermissions("project:report:remove")*/
    @Log(title = "事件上报信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        for(int i=0;i<ids.length;i++){
            HeEventReport heEventReport=heEventReportService.selectHeEventReportById(ids[i]);
            HeEventBasic heEventBasic=new HeEventBasic();
            heEventBasic.setReportEventNumber(heEventReport.getReportEventNumber());
            List<HeEventBasic> list = heEventBasicService.selectHeEventBasicList(heEventBasic);
            if(list.size()>0){
                heEventBasicService.deleteHeEventBasicById(list.get(0).getId());
            }
            HeEventFlow heEventFlow=new HeEventFlow();
            heEventFlow.setReportEventId(ids[i]);
            List<HeEventFlow> flowList=heEventFlowService.selectHeEventFlowList(heEventFlow);
            if(flowList.size()>0){
                for(int j=0;j<flowList.size();j++){
                    heEventFlowService.deleteHeEventFlowById(flowList.get(j).getId());
                }
            }

        }
        return toAjax(heEventReportService.deleteHeEventReportByIds(ids));
    }
}
